package collection.MyList;

import java.util.Arrays;

public class MyStack {
    private int[] elem;
    private  int usedSize;
    private static final int DEFAULT_CAPACITY = 10;
    public MyStack(){
        this.elem = new int[DEFAULT_CAPACITY];
    }
    public void push(int val){
        if (isFull()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = val;
        usedSize++;
    }
    public boolean isFull(){
        if (usedSize == elem.length){
            return true;
        }
        return false;
    }
    public int pop(){
        if (isEmpty()){
            throw new EmptyException("栈为空");
        }
        int oldVal =  elem[usedSize - 1];
        usedSize--;
        return oldVal;
    }
    public int peek(){
        if (isEmpty()){
            throw new EmptyException("栈为空");
        }
        return elem[usedSize - 1];
    }
    private boolean isEmpty(){
        return usedSize == 0;
    }

}
